package com.zhaotianhao.servicecart.controller;

import cn.hutool.jwt.JWT;
import com.zhaotianhao.servicecart.api.UserLoginApi;
import com.zhaotianhao.servicecart.common.JsonResult;
import com.zhaotianhao.servicecart.common.WebUtils;
import com.zhaotianhao.servicecart.dto.CartDto;
import com.zhaotianhao.servicecart.service.CartService;
import com.zhaotianhao.servicecart.vo.CartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Z-7
 * @Date 2022/4/28
 */
@Slf4j
@RestController
@RequestMapping("cart")
public class CartController {
    @Value("${jwt.key}")
    private String jwtKey;
    @Autowired
    private UserLoginApi userLoginApi;
    @Autowired
    private CartService cartService;

    @PostMapping("editCart")
    public JsonResult editCart(@RequestBody CartDto cartDto) {
        String token = WebUtils.getRequest().getHeader("token");
        JWT jwt = JWT.of(token).setKey(jwtKey.getBytes());
        long userId = Long.parseLong(jwt.getPayload("userId") + "");
        cartDto.setUserId(userId);
        // 存入redis <加入购物车按钮> 一次一件
        if (cartDto.getNumber() == null) {
            // 设置默认值 防止空指针异常
            cartDto.setNumber(1L);
            cartService.editCart(cartDto);
        }
        return JsonResult.successd();
    }
    
    @GetMapping("getOrderCart")
    public CartVo getOrderCart(Long userId) {
        // 用户登录成功 要获取自己的购物车
        CartVo orderCart = cartService.getMyCart(userId);
        return orderCart;
    }

    @GetMapping("getMyCart")
    public JsonResult getMyCart() {
        String token = WebUtils.getRequest().getHeader("token");
        JWT jwt = JWT.of(token).setKey(jwtKey.getBytes());
        long userId = Long.parseLong(jwt.getPayload("userId") + "");

        // 用户登录成功 要获取自己的购物车
        CartVo myCart = cartService.getMyCart(userId);
        return JsonResult.successd(myCart);
    }

    @PostMapping("changeCart")
    public JsonResult changeCart(@RequestBody CartDto cartDto) {
        String token = WebUtils.getRequest().getHeader("token");
        JWT jwt = JWT.of(token).setKey(jwtKey.getBytes());
        long userId = Long.parseLong(jwt.getPayload("userId") + "");
        cartDto.setUserId(userId);
        cartService.changeCart(cartDto);
        return JsonResult.successd();
    }

    @PostMapping("deleteBySkuId")
    public JsonResult deleteBySkuId(@RequestBody CartDto cartDto) {
        String token = WebUtils.getRequest().getHeader("token");
        JWT jwt = JWT.of(token).setKey(jwtKey.getBytes());
        long userId = Long.parseLong(jwt.getPayload("userId") + "");
        Boolean result = cartService.deleteBySkuId(userId,cartDto.getSkuId());
        if (result) {
            return JsonResult.successd();
        }
        return JsonResult.faild("操作失败");
    }

    @PostMapping("deleteBySkuIds")
    public JsonResult deleteBySkuIds(@RequestBody CartDto cartDto) {

        Boolean result = cartService.deleteBySkuIds(cartDto.getUserId(),cartDto.getSkuIds());
        if (result) {
            return JsonResult.successd();
        }
        return JsonResult.faild("操作失败");
    }
}
